package com.ay.weixin.fdc.boss.menu.domain;

import java.util.Iterator;
import java.util.List;

import org.lizar.config.keys.Keys;
import org.lizar.json.JList;
import org.lizar.json.JObject;
import org.lizar.util.StringHelper;

public class Menu {

	private String key;
	
	private String type;
	
	private String name;
	
	private String url;
	
	private int pos;
	
	private long wxproject_id;
	
	private List<MenuSub> subMenus;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public List<MenuSub> getSubMenus() {
		return subMenus;
	}

	public void setSubMenus(List<MenuSub> subMenus) {
		this.subMenus = subMenus;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	
	
	public int getPos() {
		return pos;
	}

	public void setPos(int pos) {
		this.pos = pos;
	}
	
	

	public long getWxproject_id() {
		return wxproject_id;
	}

	public void setWxproject_id(long wxproject_id) {
		this.wxproject_id = wxproject_id;
	}

	public JObject toJSON(){
		JObject meun=new JObject();
		meun.put("name", name);
		if(subMenus==null||subMenus.isEmpty()){
			meun.put("type", type);
			if(StringHelper.equals(type, "click")){
				meun.put("key", key);
			}else if(StringHelper.equals(type, "view")){
				meun.put("url", url);
			}else{
				System.out.println("Menu key:"+key+" has unsuppored type:"+type);
			}
		}else{
			meun.put("sub_button", subMenuToJSON(subMenus));
		}
		return meun;
	}
	
	private JList subMenuToJSON(List<MenuSub> slist){
		Iterator<MenuSub> itr=slist.iterator();
		JList l=new JList();
		MenuSub sub=null;
		int i=1;
		for(;itr.hasNext();){
			if(i>Keys.getInt("Max_Sub_Menu_Number")){
				System.err.println("Warnning: Sub Menu max number is "+Keys.getInt("Max_Sub_Menu_Number")+",the excced sub menus of menu:"+key+" will be ignored.");
				break;
			}else{
				sub=itr.next();
				JObject smenu=new JObject();
				smenu.put("name", sub.getName());
				smenu.put("type", sub.getType());
				if(StringHelper.equals(sub.getType(), "click")){
					smenu.put("key", sub.getKey());
				}else if(StringHelper.equals(sub.getType(), "view")){
					smenu.put("url", sub.getUrl());
				}else{
					System.out.println("Sub Menu key:"+key+" has unsuppored type:"+type);
				}
				l.add(smenu);
			}
			i++;
		}
		return l;
	}
	
	
}
